import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class ThemeController extends GetxController {
  final _box = GetStorage();
  final _key = 'isDarkMode';

  late ThemeMode themeMode;

  @override
  void onInit() {
    bool isDark = _box.read(_key) ?? false;
    themeMode = isDark ? ThemeMode.dark : ThemeMode.light;
    super.onInit();
  }

  bool get isDarkMode => themeMode == ThemeMode.dark;

  void toggleTheme() {
    themeMode = isDarkMode ? ThemeMode.light : ThemeMode.dark;
    Get.changeThemeMode(themeMode);
    _box.write(_key, isDarkMode);
    update(); // GetBuilder 监听
  }
}
